// ==UserScript==
// @name         app.box批量下载
// @version      0.1
// @description  try to take over the world!
// @author       wc
// @match        https://app.box.com/s/*/folder/*
// @icon         https://www.google.com/s2/favicons?domain=box.com
// @grant        none
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js
// ==/UserScript==

(function() {
    'use strict';
    var pageCount = 1
    console.log(pageCount)
    setTimeout(function(){
        pageCount = $('.btn.pagination-menu-button').text().split(' ')[0]
        console.log(pageCount)
        $('.action-bar-actions.prevent-item-deselection').append('<button class="btn bulk-download"><span class="btn-content"><span>批量下载</span></span></button>')
    },3000)

    $(document).on('click','.bulk-download',function(){
        pages(1)
    })

    function pages(page){
        if(page>pageCount){
            return
        }
        setTimeout(function(){
             var $eleList = $('.btn.more-options-btn')
            download($eleList,0,function(){
                $('.btn.next-page-btn')[0].click()
                setTimeout(function(){
                    pages(page+1)
                },3000)
            })
        },3000)
    }
    function download($elem,index,callBack){
        if($elem.length === index){
            callBack()
            return
        }
        $elem[index].click()
        setTimeout(function(){
            $(".menu-item.DownloadMenuItem").click()
            setTimeout(function(){
                $(".modal-close-button").click()
            },1000)
            setTimeout(function(){
                download($elem,index+1)
            },10000)
        },100)
    }

    // Your code here...
})();